//
//  SurpriseDidJieXiaoCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/5.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class SurpriseDidJieXiaoCell: BaseTableViewCell {
    
    @IBOutlet weak var topLabel: UILabel!
    
    @IBOutlet weak var middleLabel: UILabel!
    
    @IBOutlet weak var bottomLabel: UILabel!
    
    @IBOutlet weak var indinator: UIActivityIndicatorView!
    
    @IBOutlet weak var leftImageView: UIImageView!
    
    @IBOutlet weak var bottomLetfLabel: UILabel!

    
    @IBOutlet weak var bottomRightLabel: UILabel!

    
    var model : DidAnnounModel? {
        
        didSet{
            
            if  model == nil {
                return
            }
            
            
            topLabel.text = model?.goods_name
            
            
            indinator.startAnimating()
            leftImageView.sd_setImage(with: URL(string :model!.goods_image  ), placeholderImage: UIImage.defaulLogoImage(), options: []) { (image, _, _, _) in
                self.indinator.stopAnimating()
                
                
            }
            
            
            middleLabel.attributedText = attributedString(str: "本期期号: \(model!.od_no)", color: UIColor.red)
            
            
            bottomLabel.attributedText = attributedString(str: "我已参与: \(model!.my_count)人次", color: UIColor.orange)
            
            
            bottomLetfLabel.attributedText = attributedString(str: "获奖者: \(model!.member_name)", color: UIColor.orange)
            
            bottomRightLabel.attributedText = attributedString(str: "参与人数: \(model!.join_member_count)人", color: UIColor.orange)
            
            
        }
        
        
        
        
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    
    
    
    func attributedString(str: String, color: UIColor) -> NSAttributedString? {
        
        
        //创建正则表达式对象
        let patten0 = "[\u{4e00}-\u{9fa5}]{1,}"
        //创建正则表达式对象
        let patten1 = "[:]"
        
        //        let patten2 = "[¥]"    + "|" +  patten2
        
        let patten = patten0 + "|" + patten1
        
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 14), range:NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: color, range:NSMakeRange(0, str.characters.count) )
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.dakGray(), range:temp.range )
            
        }
        
        return strM
        
    }
    


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
